package com.abu.notice.controller;


import com.abu.common.Resp;
import com.abu.notice.config.mvc.LoginInterceptor;
import com.abu.notice.pojo.Notice;
import com.abu.notice.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-04-29
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("noAuthorize/queryPage/{page}/{size}")
    public Resp queryByPage(
            @PathVariable("page") Long page,
            @PathVariable("size") Long size
    ) {
        HashMap<String, Object> noticeMapData = this.noticeService.selectByPage(page, size);
        return Resp.ok().data(noticeMapData);
    }

    @GetMapping("noAuthorize/queryPage/all/{page}/{size}")
    public Resp queryAllByPage(
            @PathVariable("page") Long page,
            @PathVariable("size") Long size
    ) {
        HashMap<String, Object> noticeMapData = this.noticeService.selectAllByPage(page, size);
        return Resp.ok().data(noticeMapData);
    }


    @GetMapping("noAuthorize/queryByCondition")
    public Resp queryByPage(@RequestParam(value = "key", required = false) String key) {
        List<Notice> noticeList = this.noticeService.selectByKey(key);
        long count = noticeList.stream().count();
        if (CollectionUtils.isEmpty(noticeList)) {
            return Resp.error().msg("公告数据不存在").code(404);
        }
        return Resp.ok().data("noticeList", noticeList).data("count", count);
    }

    @GetMapping("noAuthorize/all/condition")
    public Resp queryAllMatchedKey(@RequestParam(value = "key") String key) {
        List<Notice> noticeList = this.noticeService.selectAllMatched(key);
        if (CollectionUtils.isEmpty(noticeList)) {
            return Resp.error().msg("相应数据不存在").code(404);
        }
        return Resp.ok().data("noticeList", noticeList);
    }

    @GetMapping("noAuthorize/latest")
    public Resp queryLatestNoticeList() {
        List<Notice> noticeList = this.noticeService.selectLatestNotice();
        if (CollectionUtils.isEmpty(noticeList)) {
            return Resp.error().code(404).msg("内容为空");
        }
        return Resp.ok().data("noticeList", noticeList);
    }

    @GetMapping("noAuthorize/id/{noticeId}")
    public Resp queryNoticeByIdIsPublish(@PathVariable("noticeId") Long noticeId) {
        Notice notice = this.noticeService.selectById(noticeId);
        if (notice == null) {
            return Resp.error().code(404).msg("内容为空");
        }
        return Resp.ok().data("notice", notice);
    }

    @GetMapping("noAuthorize/all/id/{noticeId}")
    public Resp queryNoticeByIdYesOrNotPublish(@PathVariable("noticeId") String noticeId) {
        Notice notice = this.noticeService.getOne(new QueryWrapper<Notice>().eq("id", noticeId).select("content", "title"));
        if (notice == null) {
            return Resp.error().code(404).msg("内容为空");
        }
        return Resp.ok().data("notice", notice);
    }

    @PostMapping("entity")
    public Resp saveNotice(@RequestBody Notice notice) {
        Boolean flag = this.noticeService.saveNotice(notice);
        if (flag) {
            return Resp.ok().msg("保存成功");
        }
        return Resp.error().msg("添加失败");
    }

    @PutMapping("entity")
    public Resp updateNoticeByNoticeId(@RequestBody Notice notice) {
        boolean flag = this.noticeService.updateNoticeById(notice);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }

    @PutMapping("publish/{id}/{status}")
    public Resp updateNoticeStatusByNoticeId(@PathVariable("id") Long id, @PathVariable("status") Boolean status) {
        boolean flag = this.noticeService.updateNoticeStatusByNoticeId(id,status);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }

    @DeleteMapping("id/{noticeId}")
    public Resp deleteNoticeByNoticeId(@PathVariable("noticeId") Long noticeId) {
        boolean flag = this.noticeService.removeNoticeById(noticeId);
        if (flag) {
            return Resp.ok().msg("删除成功");
        }
        return Resp.error().msg("修改失败");
    }

}

